package com.anlen.call.ui;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.Header;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.anlen.call.R;
import com.anlen.call.api.CallApi;
import com.anlen.call.base.BaseActivity;
import com.anlen.call.bean.People;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class IndexActivity extends BaseActivity {

	public static final int NETWORKTYPE_INVALID = 0;  
	/** wap网络 */  
	public static final int NETWORKTYPE_WAP = 1;  
	/** 2G网络 */  
	public static final int NETWORKTYPE_2G = 2;  
	/** 3G和3G以上网络，或统称为快速网络 */  
	public static final int NETWORKTYPE_3G = 3;  
	/** wifi网络 */  
	public static final int NETWORKTYPE_WIFI = 4; 
	
	private EditText et,et2,et3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
		Button bt = (Button)findViewById(R.id.callButton);
		Button bt2 = (Button)findViewById(R.id.getdata);
		
		bt.setOnClickListener(new DailListener());
        bt2.setOnClickListener(new DataListener());
		
		et = (EditText)IndexActivity.this.findViewById(R.id.dailNumber);
		et2 = (EditText)IndexActivity.this.findViewById(R.id.network);
		et3 = (EditText)IndexActivity.this.findViewById(R.id.data);
     	TextView tv=(TextView)IndexActivity.this.findViewById(R.id.tvPublicTiTle);
     	tv.setText("首页");
        String str2 = NetWorkType(this);
      	et2.setText(str2);
    }

  	private class DailListener implements OnClickListener{
  		@Override
  		public void onClick(View v) {
  			
  			String str = et.getText().toString().trim();
  			if(TextUtils.isEmpty(str)){
  				showToast("号码不能为空");
  				return ;
  			}
  			
  			Intent intent = new Intent();
  			intent.setAction(Intent.ACTION_CALL);
  			intent.setData(Uri.parse("tel:"+str));
  			
  			startActivity(intent);
  		}
   }
    
    private class DataListener implements OnClickListener{
    	@Override
		public void onClick(View v) {
			CallApi.apistart(ahph);
			
		}
    };
  	
  	//检查网络的快慢
  	private static boolean isFastMobileNetwork(Context context) {  
  		TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);  
  		switch (telephonyManager.getNetworkType()) {  
	       case TelephonyManager.NETWORK_TYPE_1xRTT:  
	           return false; // ~ 50-100 kbps  
	       case TelephonyManager.NETWORK_TYPE_CDMA:  
	           return false; // ~ 14-64 kbps  
	       case TelephonyManager.NETWORK_TYPE_EDGE:  
	           return false; // ~ 50-100 kbps  
	       case TelephonyManager.NETWORK_TYPE_EVDO_0:  
	           return true; // ~ 400-1000 kbps  
	       case TelephonyManager.NETWORK_TYPE_EVDO_A:  
	           return true; // ~ 600-1400 kbps  
	       case TelephonyManager.NETWORK_TYPE_GPRS:  
	           return false; // ~ 100 kbps  
	       case TelephonyManager.NETWORK_TYPE_HSDPA:  
	           return true; // ~ 2-14 Mbps  
	       case TelephonyManager.NETWORK_TYPE_HSPA:  
	           return true; // ~ 700-1700 kbps  
	       case TelephonyManager.NETWORK_TYPE_HSUPA:  
	           return true; // ~ 1-23 Mbps  
	       case TelephonyManager.NETWORK_TYPE_UMTS:  
	           return true; // ~ 400-7000 kbps  
	       case TelephonyManager.NETWORK_TYPE_EHRPD:  
	           return true; // ~ 1-2 Mbps  
	       case TelephonyManager.NETWORK_TYPE_EVDO_B:  
	           return true; // ~ 5 Mbps  
	       case TelephonyManager.NETWORK_TYPE_HSPAP:  
	           return true; // ~ 10-20 Mbps  
	       case TelephonyManager.NETWORK_TYPE_IDEN:  
	           return false; // ~25 kbps  
	       case TelephonyManager.NETWORK_TYPE_LTE:  
	           return true; // ~ 10+ Mbps  
	       case TelephonyManager.NETWORK_TYPE_UNKNOWN:  
	           return false;  
	       default:  
	           return false;  
	    }  
	}  
  	
  	/** 
     * 获取网络状态，wifi,wap,2g,3g. 
     * 
     * @param context 上下文 
     * @return int 网络状态       
     */  
  
    public static int getNetWorkType(Context context) {  
    	int mNetWorkType=0;
  
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();  
  
        if (networkInfo != null && networkInfo.isConnected()) {  
            String type = networkInfo.getTypeName();  
  
            if (type.equalsIgnoreCase("WIFI")) {  
                mNetWorkType = NETWORKTYPE_WIFI;  
            } else if (type.equalsIgnoreCase("MOBILE")) {  
                @SuppressWarnings("deprecation")
				String proxyHost = android.net.Proxy.getDefaultHost();  
  
                mNetWorkType = TextUtils.isEmpty(proxyHost)  
                        ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G)  
                        : NETWORKTYPE_WAP;  
            }  
        } else {  
            mNetWorkType = NETWORKTYPE_INVALID;  
        }  
  
        return mNetWorkType;  
    }   
    
  	//网络信号转化为字符串
  	private String NetWorkType(Context mcontext) {  
  		String netType="";
  		switch(getNetWorkType(mcontext)){
	  		case NETWORKTYPE_INVALID: 
	  			netType="网络没信号";
	  			break;
	  		case NETWORKTYPE_WIFI: netType="wifi信号";break;
	  		case NETWORKTYPE_2G: netType="2G信号";break;
	  		case NETWORKTYPE_3G: netType="3G信号";break;
	  		case NETWORKTYPE_WAP: netType="wap信号";break;
	  		default: break;
  		}
  	        return netType;
  	}
  	
  	private final AsyncHttpResponseHandler ahph=new AsyncHttpResponseHandler(){

		@Override
		public void onFailure(int statusCode, Header[] header, byte[] body,Throwable exception) {
			exception.printStackTrace();
			et3.setText("真失败！");
		}

		@Override
		public void onSuccess(int statusCode, Header[] header, byte[] body) {
			if(200 == statusCode){
				String str="";
				//字节转化为String
				Gson gson = new Gson();
				String jsonRes ="";
				try {
					jsonRes = new String(body,"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				Log.d("json","jsonRes="+jsonRes);
				
				List<People> list =gson.fromJson(jsonRes, new TypeToken<List<People>>() {}.getType());
				for(People p:list){
					str+=p.getId()+" "+p.getName()+"  "+p.getSex()+" ";
				}
				Log.d("jsonStr","jsonStr="+str);
				
				et3.setText(str);
			}
		}
  	};
  	
  	
}

    

